<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>内联模板&动态组件</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../style.css">
</head>

<body>
    <!--[if lt IE 7]>
        <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->
    <div id="app">

        <p>内联模板：在html中定义模板</p>
        <child-component inline-template>
            <div class="component">
                <h2>在父组件中定义子组件的模板</h2>
                <p class="main">{{ message }}</p>
                <p>{{ msg }}</p>
            </div>
        </child-component>

        <hr>

        <p>动态组件：动态挂在不同的组件</p>
        <component :is="currentView"></component>
        <button @click="handleChangeView('comA')">切换到A</button>
        <button @click="handleChangeView('comB')">切换到B</button>
        <button @click="handleChangeView('comC')">切换到C</button>

        <hr>

        <p>异步组件：动态解析组件，需要渲染时触发渲染</p>
        <async-component></async-component>

    </div>

    <script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js'></script>
    <script src="" async defer></script>
    <script>
        Vue.component('child-component', {
            data: function(){
                return {
                    msg: '在子组件中的数据'
                }
            }
        })

        Vue.component('async-component', 
            function (resolve, reject) {
                setTimeout(function () {  
                    resolve({
                        template: '<div class="component">我是异步渲染的组件</div>'
                    })
                }, 2000)
            }
        )

        var app = new Vue({
            el: '#app',
            data: {
                message: '在父组件声明的数据',
                currentView: 'comA'
            },
            components: {
                comA: {
                    template: '<div class="component">组件A</div>'
                },
                comB: {
                    template: '<div class="component">组件B</div>'
                },
                comC: {
                    template: '<div class="component">组件C</div>'
                }
            },
            methods: {
                handleChangeView: function(component){
                    console.log(component);
                    this.currentView = component
                }
            }
        })
    </script>
</body>

</html>